package com.uzai.console.web.groupsendjob;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.groupsendjob.*;
import com.uzai.console.enums.GroupSendJobSourceEnum;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.JdTokenMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.device.DeviceTypeService;
import com.uzai.console.service.groupsendjob.GroupSendJobService;
import com.uzai.console.service.order.OrderReStatusService;
import com.uzai.console.service.order.OrderSourceService;
import com.uzai.console.service.order.OrderStatusService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.groupsendjob.GroupSendCountVo;
import com.uzai.console.vo.groupsendjob.GroupSendJobInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 群发任务
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "群发任务",tags = "客户维护|群发任务")
@ApiSort(28)
@RestController
public class GroupSendJobController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(GroupSendJobController.class);

    @Autowired
    private GroupSendJobService groupSendJobService;
    @Autowired
    private OrderStatusService orderStatusService;
    @Autowired
    private OrderReStatusService orderReStatusService;
    @Autowired
    private OrderSourceService orderSourceService;
    @Autowired
    private JdTokenMapper jdTokenMapper;
    @Autowired
    private DeviceWechatInfoService deviceWechatInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "查询群发类型列表", notes = "查询群发类型列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/groupSendJob/findGroupSourceListSelected")
    public Object findGroupSourceListSelected(@RequestBody GroupSendJobQuery groupSendJobQuery){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        for (GroupSendJobSourceEnum groupSendJobSourceEnum : GroupSendJobSourceEnum.values()){
            SelectKeyVo selectKeyVo = new SelectKeyVo();
            selectKeyVo.setKey(String.valueOf(groupSendJobSourceEnum.getId()));
            selectKeyVo.setValue(groupSendJobSourceEnum.getDesc());
            selectKeyVoList.add(selectKeyVo);
        }
        return selectKeyVoList;
    }

    @ApiOperation(value = "查询群发任务列表", notes = "查询群发任务列表", response = GroupSendJobInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/groupSendJob/findGroupSendJobList")
    public Object findGroupSendJobList(@RequestBody GroupSendJobQuery groupSendJobQuery){
        groupSendJobQuery.setParentId(0L); //只查询主任务
        return groupSendJobService.findGroupSendJobList(groupSendJobQuery);
    }

    @ApiOperation(value = "查询群发任务详情", notes = "查询群发任务详情", response = GroupSendJobInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/groupSendJob/findGroupSendJobInfo")
    public Object findGroupSendJobInfo(@RequestBody GroupSendJobIdDto groupSendJobIdDto){
        return groupSendJobService.findGroupSendJobInfo(groupSendJobIdDto);
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "查询添加群发任务总数", notes = "查询添加群发任务总数",response = GroupSendCountVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/groupSendJob/findAddGroupSendJobCount")
    public Object findAddGroupSendJobCount(@RequestBody GroupSendJobAddDto groupSendJobAddDto){
        return groupSendJobService.findAddGroupSendJobCount(groupSendJobAddDto);
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "添加群发任务", notes = "添加群发任务")
    @ApiOperationSupport(order = 3)
    @PostMapping("/groupSendJob/addGroupSendJob")
    public Object addGroupSendJob(@RequestBody GroupSendJobAddDto groupSendJobAddDto){
        groupSendJobService.addGroupSendJob(groupSendJobAddDto, getLoginInfo().getLoginName());
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改群发任务", notes = "修改群发任务")
    @ApiOperationSupport(order = 4)
    @PostMapping("/groupSendJob/updateGroupSendJob")
    public Object updateGroupSendJob(@RequestBody GroupSendJobUpdateDto groupSendJobUpdateDto){
        groupSendJobService.updateGroupSendJob(groupSendJobUpdateDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "开始群发任务", notes = "开始群发任务")
    @ApiOperationSupport(order = 5)
    @PostMapping("/groupSendJob/startGroupSendJob")
    public Object startGroupSendJob(@RequestBody GroupSendJobIdDto groupSendJobIdDto){
        groupSendJobService.startGroupSendJob(groupSendJobIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "暂停群发任务", notes = "暂停群发任务")
    @ApiOperationSupport(order = 5)
    @PostMapping("/groupSendJob/pauseGroupSendJob")
    public Object pauseGroupSendJob(@RequestBody GroupSendJobIdDto groupSendJobIdDto){
        groupSendJobService.pauseGroupSendJob(groupSendJobIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "结束群发任务", notes = "结束群发任务")
    @ApiOperationSupport(order = 6)
    @PostMapping("/groupSendJob/finishGroupSendJob")
    public Object finishGroupSendJob(@RequestBody GroupSendJobIdDto groupSendJobIdDto){
        groupSendJobService.finishGroupSendJob(groupSendJobIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除群发任务", notes = "删除群发任务")
    @ApiOperationSupport(order = 7)
    @PostMapping("/groupSendJob/deleteGroupSendJob")
    public Object deleteGroupSendJob(@RequestBody GroupSendJobIdDto groupSendJobIdDto){
        groupSendJobService.deleteGroupSendJob(groupSendJobIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.GroupSendJob, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "修复通讯录群发任务", notes = "修复通讯录群发任务")
    @ApiOperationSupport(order = 7)
    @PostMapping("/groupSendJob/repairWxContactGroupSendJob")
    public Object repairWxContactGroupSendJob(@RequestBody GroupSendJobRepairDto groupSendJobRepairDto){
        groupSendJobService.repairWxContactGroupSendJob(groupSendJobRepairDto);
        return true;
    }


}
